﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace RoadPlot
{
    public static class Road
    {
        private static PathGeometry GetGeometry(RoadRoute rr)
        {
            var myPathFigure = new PathFigure { StartPoint = rr._points[0] };

            BezierSpline.AddSegments(rr._points, myPathFigure.Segments);

            var myPathGeometry = new PathGeometry();
            myPathGeometry.Figures.Add(myPathFigure);

            // Display the "poly bezier". 
          
            return myPathGeometry;
        }

        public static void DrawBorderedRoutes(IEnumerable<RoadRoute> routes, Canvas canvas, bool isProblem)
        {
            if (isProblem)
            {
                DrawRoad(routes, new SolidColorBrush(Color.FromArgb(200, 255, 0, 0)), canvas, -4);
                DrawRoadLegend(routes, canvas);
                return;
            }
            DrawRoad(routes, new SolidColorBrush(Color.FromRgb(0, 0, 0)),  canvas, 0);
            DrawRoad(routes, new SolidColorBrush(Color.FromRgb(220, 200, 200)),  canvas, 4);
            DrawRoadLegend(routes, canvas);
        }

        private static void DrawRoad(IEnumerable<RoadRoute> routes, Brush colorfill, Canvas canvas, double trim)
        {
            foreach (var route in routes)
            {
                double w = route._roadWidth - trim;
                if (w > 0)
                {
                    DrawRoadSurface(route, colorfill, w, canvas);
                }
            }
        }

        private static void DrawRoadSurface(RoadRoute rr, Brush colorfill, double width1, Canvas canvas)
        {
            var myPathFigure = new PathFigure { StartPoint = rr._points[0] };

            BezierSpline.AddSegments(rr._points, myPathFigure.Segments);

            var myPathGeometry = new PathGeometry();
            myPathGeometry.Figures.Add(myPathFigure);

            // Display the "poly bezier". 
            var myPath = new Path
            {
                Stroke = colorfill,
                StrokeThickness = width1,
                Data = myPathGeometry,
                StrokeStartLineCap = PenLineCap.Round,
                StrokeEndLineCap = PenLineCap.Round,
                StrokeLineJoin = PenLineJoin.Round
            };

            canvas.Children.Add(myPath);
        }

        private static void DrawRoadLegend(IEnumerable<RoadRoute> routes, Canvas canvas)
        {
            foreach (var route in routes)
            {
                PathGeometry myPathGeometry = GetGeometry(route);

                if (route._legends != null)
                {
                    var curvyText = new CurvyText();

                    foreach (var legend in route._legends)
                    {
                        curvyText.PrepareCurvyText(myPathGeometry, route._baseLineTrim, new FontFamily("Verdana"), route._fontSize, legend);
                        curvyText.AddCurvyTextBlocks(canvas.Children);
                    }
                }
            }
        }
    }
}
